uname_s := $(shell uname -s)
uname_m := $(shell uname -m)

wsl := $(shell uname -v | grep Microsoft -o)

src := $(wildcard *.c)
target := $(patsubst %.c,%.o,${src})

path := $(shell pwd)

GCC := gcc
CLANG := clang 
MSVC := /mnt/e/tools/Microsoft\ Visual\ Studio/2022/Community/VC/Tools/MSVC/14.36.32532/bin/Hostx64/x64/cl.exe
# GCC := x86_64-w64-mingw32-gcc

PREDEFINED := -DPTHREADALT_INTERNAL -D__USE_GNU

# wsl 不能使用RSEQ
ifeq ($(wsl),Microsoft)
	PREDEFINED := ${PREDEFINED} -DDONOT_USE_RSEQ -DWSLINUX
	GCC := gcc-12
$(info detected WSL1(Windows Subsystem Linux))
endif

# SANITIZE_GCC := -fsanitize=undefined 
#-fsanitize=address 

# 不告警注释
# -Wpedantic 这个严格按照ISO C标准
CWARN := -Wextra -Wfloat-equal #-Werror -Wall #-Wpedantic
CWARN_EXCEP := -Wno-error=cpp -Wno-error=comment -Wno-comment -Wno-gnu-zero-variadic-macro-arguments	\
	-Wno-language-extension-token -Wno-error=pedantic						\
	-Wno-gnu-statement-expression -Wno-error=unused-but-set-variable

CFLAGS := ${CWARN} ${CWARN_EXCEP} -fno-keep-static-consts -std=gnu11
#-Wno-implicit-function-declaration
# -Wno-unused-parameter 
# -fno-keep-static-consts 即使不开启优化也不生成static const
#-Wno-undefined-internal
# -fms-extensions 允许自动展开继承结构体
#-std=c11 -Werror -Wno-undefined-internal
# -ftrack-macro-expansion=256 让宏错误显示的更明确

GCCFLAGS :=  -ftrack-macro-expansion=256 
GCCANALYZER := -fanalyzer -Wno-analyzer-use-of-uninitialized-value
#-Wno-analyzer-null-dereference
# -Wno-analyzer-null-dereference 似乎是误报
#-Wno-error=analyzer-use-of-uninitialized-value -fanalyzer 

CLANGFLAGS := 
#CLANGANALYZER := --analyzer-max-loop 20 --analyzer-inline-max-stack-depth 20 --analyzer-max-nodes 0 --analyzer-inline-max-function-size 65535 --analyzer-opt-analyze-headers

# -Wimplicit-function-declaration WSL消除告警
# -fsemantic-interposition 
# g1 最小等级 g3支持宏展开
COPT_FLAGS := -O3 -fmerge-all-constants -g3 #-funroll-loops 



ifeq ($(uname_s),Darwin)
	core_nr := $(shell sysctl -n hw.ncpu)
	cpu_model := 
	sharelib := dylib
	CLANG := /opt/homebrew/opt/llvm/bin/clang
	GCC := llvm-gcc
	GCCFLAGS :=  
	GCCANALYZER := 
	clang_version := $(shell ${CLANG} -v)
else
	ifeq ($(uname_s),Linux)
		core_nr := $(shell nproc)
		cpu_model := $(shell cat /proc/cpuinfo | grep "model name" | grep -e ": [0-9a-zA-Z()-].*" -o | head -n 1 )
		sharelib := so
	else
		$(info can not get cpu model)
	endif
endif

ifeq ($(uname_m),aarch64)
	COPT_FLAGS := ${COPT_FLAGS} -march=armv8-a+lse
	# 默认使用 SM8350
	cpu_model := SM8350
endif

ifeq ($(uname_m),arm64)
	COPT_FLAGS := ${COPT_FLAGS} -march=armv8-a+lse
endif

$(info cpu_model ${cpu_model}, core_nr [${core_nr}], clang version ${clang_version} )

is_intel := $(shell echo "${cpu_model}" | grep -o "Intel" )

# 并行make 
MAKEFLAGS += -j${core_nr}


ifeq ($(cpu_model),: 13th Gen Intel(R) Core(TM) i7-13700K)
	PREDEFINED := ${PREDEFINED} -DHDEF_CPU_MODEL_CORE_13700K -DHDEF_LITTLECORE_OFFST=16 -DHDEF_LITTLECORE_NR=8 
endif
ifeq ($(cpu_model),: AMD Ryzen 7 3700X 8-Core Processor)
	PREDEFINED := ${PREDEFINED} -DHDEF_CPU_MODEL_RYZEN_3700X -DHDEF_LITTLECORE_OFFST=0 -DHDEF_LITTLECORE_NR=0
endif
ifeq ($(cpu_model),SM8350)
	# 不能使用RSEQ
	PREDEFINED := ${PREDEFINED} -DHDEF_CPU_MODEL_SM8350 -DHDEF_LITTLECORE_OFFST=0 -DHDEF_LITTLECORE_NR=4 -DDONOT_USE_RSEQ
endif




# disable shadow stack
ifeq ($(uname_m),x86_64)
	# -mprfchw 启用预取
	COPT_FLAGS := ${COPT_FLAGS} -mprfchw -mcx16 -mlzcnt -fcf-protection=none -fno-stack-protector -mno-mmx -mno-3dnow -mno-avx -mno-avx2
	# -mno-sse -mno-sse2 -mno-mmx -mno-3dnow
endif

